import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login"
import Home from "../components/Home"
import Welcome from "../components/Welcome"
import Users from "../components/Users/Users"
import Rights from "../components/Power/Rights"
import Roles from "../components/Power/Roles"
import Cate from "../components/Goods/Cate"
import Params from "../components/Goods/Params"
import List from "../components/Goods/List"
import Add from "../components/Goods/Add"
import Order from "../components/Order/Order"
import Report from "../components/Report/Report"
Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    component: Login,
    name: "login",
  },
  {
    path: "/login",
    redirect: "/",
  },
  {
    path: "/home",
    component: Home,
    name: "home",
    // redirect:"/welcome",
    //点击用户列表。/users
    children: [
      {
        path: "",
        component: Welcome,
      },
      {
        path: "/welcome",
        component: Welcome,
        name: "welcome",
      },
      {
        path: "/users",
        component: Users,
        name: "users",
      },
      {
        path: "/rights",
        component: Rights,
        name: "userightsrs",
      },
      {
        path:"/roles",
        component:Roles,
        name:"roles"
      },
      {
        path:"/categories",
        component:Cate,
        name:"cate"
      },
      {
        path:"/params",
        component:Params,
        name:"params"
      },
      {
        path:"/goods",
        component:List,
        name:"list"
      },
      {
        path:"/goods/add",
        component:Add,
        name:"add"
      },
      {
        path:"/orders",
        component:Order,
        name:"order"
      },
      {
        path:"/reports",
        component:Report,
        name:"report"
      },
    ],
  },
]

const router = new VueRouter({
  routes,
})
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.name == "login") {
    return next()
  } else {
    const tokenStr = window.sessionStorage.getItem("token")
    if (!tokenStr) {
      return next("/login")
    } else {
      next()
    }
  }
})
export default router
